package com.ocean.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocean.admin.entity.SysOperLog;
import com.ocean.admin.extension.ConstantsCacheExtension;
import com.ocean.admin.mapper.SysOperLogMapper;
import com.ocean.admin.model.dto.operlog.SysOperLogQueryDTO;
import com.ocean.admin.model.vo.operlog.SysOperLogVo;
import com.ocean.admin.service.ISysDictDataService;
import com.ocean.admin.service.ISysOperLogService;
import com.ocean.core.util.constants.ConstantsAttribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 操作日志记录 服务实现类
 * </p>
 *
 * @author zhang
 * @since 2022-08-16
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLog> implements ISysOperLogService {

    @Autowired
    private ISysDictDataService iSysDictDataService;

    /**
     * 分页查询操作日志
     *
     * @param queryDTO 查询条件
     * @return 结果
     */
    @Override
    public IPage<SysOperLogVo> pageList(SysOperLogQueryDTO queryDTO) {
        Page<SysOperLogVo> page = new Page<>(queryDTO.getPage(), queryDTO.getPageSize());
        IPage<SysOperLogVo> pageList = baseMapper.pageList(page, queryDTO);
        pageList.getRecords().forEach(res -> {
            res.setBusinessTypeName(iSysDictDataService.get(ConstantsCacheExtension.getInstance().getSysOperType(), res.getBusinessType()));
            res.setOperatorTypeName(iSysDictDataService.get(ConstantsCacheExtension.getInstance().getSysOperType(), res.getOperatorType()));
            res.setStatusName(res.getStatus() == 1 ? "异常" : "正常");
        });
        return pageList;
    }

    /**
     * 删除系统日志
     *
     * @param operIds 日志ID
     * @return 结果
     */
    @Override
    public String removeOperLog(Long[] operIds) {
        List<Long> longs = Arrays.asList(operIds);
        this.removeByIds(longs);
        return ConstantsAttribute.REMOVE_SUCCESS;
    }
}
